package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.UsersDao;
import com.vo.USERS;

public class Web extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		String[] checkbox = request.getParameterValues("checkbook");
		System.out.println("bbb"+checkbox);
		UsersDao dao = new UsersDao();
		USERS user = new USERS();
		user.setName(name);
		user.setPWD(pwd);
		try {
			
			boolean flag = dao.login(user);
			if(flag){
				if(checkbox==null){
					RequestDispatcher d = request.getRequestDispatcher("welcome.jsp");
					d.forward(request, response);
					System.out.println("okas");
				}else{
					Cookie cook = new Cookie("uname", name);
					cook.setMaxAge(30*60);
					response.addCookie(cook);
					System.out.println("ok");
					RequestDispatcher d = request.getRequestDispatcher("welcome.jsp");
					d.forward(request, response);
				}
			}else{
				System.out.println("err");
				RequestDispatcher d = request.getRequestDispatcher("error.jsp");
				d.forward(request, response);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
